def isGroup(hand, n, v=False) :
    nums = map(lambda x:x[0], hand)
    for num in nums :
        count = 0
        for num2 in nums :
            if num2 == num :
                count += 1
        if count == n :
            if v : return num
            return True
    return False

def isHand(hand) :
    return True

def isPair(hand, v=False) :
    return isGroup(hand, 2, v)

def isTwoPair(hand) :
    nums = map(lambda x:x[0], hand)
    pairs = 0
    for num in nums :
        count = 0
        for num2 in nums :
            if num2 == num :
                count += 1
        if count == 2 :
            pairs += 1
    if pairs == 4 : return True
    return False

def isTriple(hand) :
    return isGroup(hand, 3)

def isStraight(hand) :
    return "".join(map(lambda x:x[0], hand)) in [
        "23456",
        "34567",
        "45678",
        "56789",
        "6789T",
        "789TJ",
        "89TJQ",
        "9TJQK",
        "TJQKA"
    ]

def isFlush(hand) :
    return "".join(map(lambda x:x[1], hand)) in [
        "CCCCC",
        "DDDDD",
        "SSSSS",
        "HHHHH"
    ]

def isFullHouse(hand) :
    return isPair(hand) and isTriple(hand)

def isFour(hand) :
    return isGroup(hand, 4)

def isStraightFlush(hand) :
    return isFlush(hand) and isStraight(hand)

def getRank(card) :
    return int({
        "T" : 10,
        "J" : 11,
        "Q" : 12,
        "K" : 13,
        "A" : 14
    }.get(card[0], card[0]))

def highCard(handType, hand1, hand2) :
    if handType == isHand :
        h1 = sorted(map(getRank, hand1))
        h2 = sorted(map(getRank, hand2))
        while h1 and h2 :
            if h1[-1] > h2[-1] :
                print h1[-1], ">", h2[-1]
                return True
            if h1[-1] < h2[-1] :
                print h1[-1], "<", h2[-1]
                return False
            h1.pop()
            h2.pop()
        return True
    if handType == isPair :
        #return highCard(isHand, hand1, hand2)
        h1 = getRank(isPair(hand1, True))
        h2 = getRank(isPair(hand2, True))
        if h1 > h2 :
            print h1, ">", h2
            return True
        if h1 < h2 :
            print h1, "<", h2
            return False
        return highCard(isHand, hand1, hand2)
    print "does player 1 have the high card?"
    print str(handType)
    print hand1
    print hand2
    if raw_input(" > ") == "y" : return True
    return False

def handBetter(hand1, hand2) :
    handTypes = [
        isStraightFlush,
        isFour,
        isFullHouse,
        isFlush,
        isStraight,
        isTriple,
        isTwoPair,
        isPair,
        isHand
    ]
    for isHandType in handTypes :
        h1 = isHandType(hand1)
        h2 = isHandType(hand2)
        if h1 and not h2 :
            print str(isHandType)
            return True
        if h2 and not h1 :
            print str(isHandType)
            return False
        if h2 and h1 :
            print str(isHandType), "highCard"
            return highCard(isHandType, hand1, hand2)

file = open("54.data", "r")
gamesWon = 0
gamesLost = 0
for game in file :
    cards = game[:-1].split(" ")
    print " ".join(cards[:5]), "|", " ".join(cards[5:])
    if handBetter(cards[:5], cards[5:]) :
        gamesWon += 1
        print "win", gamesWon
    else :
        gamesLost += 1
        print "lose", gamesLost

print "gamesWon:", gamesWon